package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.app.Constants;
import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.MyLabelsContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;
import com.hdcircles.autoform.util.SharedPreferenceUtil;

import java.util.List;

import javax.inject.Inject;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/10/17.
 */

public class MyLabelsPresenter extends RxPresenter<MyLabelsContract.View> implements MyLabelsContract.Presenter {



    private RetrofitHelper retrofitHelper;

    @Inject
    public MyLabelsPresenter(RetrofitHelper retrofitHelper){
        this.retrofitHelper=retrofitHelper;
    }


    @Override
    public void getFormData() {
//        mView.showContent(realmHelper.getInitFormList());
        FormBean params=new FormBean();
        params.setCreator(SharedPreferenceUtil.getUserEmail());
        params.setStatus(Constants.STATUS_INITIALIZATION);
        params.setTime(null);
        Subscription subscription=retrofitHelper.getFormList(params)
                .compose(RxUtil.<HttpResponse<List<FormBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<FormBean>>handleResult())
                .subscribe(new Action1<List<FormBean>>() {
                    @Override
                    public void call(List<FormBean> formBeanList) {
                        mView.showContent(formBeanList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                        mView.showEmpty();
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void deleteFormData(String id) {
//        realmHelper.deleteAllFormById(id);
    }

    @Override
    public void getSharedFormData() {
        Subscription subscription=retrofitHelper.getSharedFormList(SharedPreferenceUtil.getUserEmail())
                .compose(RxUtil.<HttpResponse<List<FormBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<FormBean>>handleResult())
                .subscribe(new Action1<List<FormBean>>() {
                    @Override
                    public void call(List<FormBean> formBeanList) {
                        mView.showSharedContent(formBeanList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void changeFormPosition(String id, long time, boolean isPlus) {

    }

    @Override
    public void saveDefaultData(FormBean formBean) {
//        realmHelper.insertForm(formBean);
        Subscription subscription=retrofitHelper.saveForm(formBean)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mView.saveDefaultDataSuccess();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                        LogUtil.e(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }
}
